package com.work.game.home.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.work.game.R;
import com.work.game.home.ui.inter.OnClickCateListener;
import com.work.game.home.ui.model.Category;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;

public class CateAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private List<Category> categoryList;
    private OnClickCateListener onClickCateListener;
    private String index = "0";

    public CateAdapter(Context context, OnClickCateListener onClickCateListener, List<Category> categoryList) {
        this.mContext = context;
        this.categoryList = categoryList;
        this.onClickCateListener = onClickCateListener;
    }


    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (ObjectUtil.isNull(convertView)) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cate, null);
        }
        TextView tvName = convertView.findViewById(R.id.name);
        Category category = (Category) getItem(position);
        if (index.equals(category.getPcSort())) {
            tvName.setBackgroundColor(mContext.getResources().getColor(R.color.bg_cate));
            tvName.setTextSize(16);
        } else {
            tvName.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            tvName.setTextSize(14);
        }
        tvName.setText(category.getPcName());
        convertView.setTag(position);
        convertView.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        Category category = (Category) getItem(position);
        index = category.getPcSort();
        if ("0".equals(index)) {
            onClickCateListener.onClickCateListener("");
        } else {
            onClickCateListener.onClickCateListener(category.getId());
        }
        notifyDataSetChanged();
    }
}
